from web.page.page import WebPage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import random
import time
class RegisterPage(WebPage):
    url = 'http://huicewang.com/ecshop/user.php?act=register'
    inputs_loc = (By.XPATH, '//form/table/tbody/tr/td/input[@type="text" or @type="password"]')

    sel_question_loc = (By.NAME,'sel_question')
    submit_loc = (By.NAME,'Submit')

    def click_summit(self):
        self.find_element(*self.submit_loc).click()
#改进前
# 1、定义定位属性在类RegisterPage中
# 2、在RegisterPage类中的click_summit方法中：根据定位属性找到对象
# 3、在RegisterPage类中的click_summit方法中：对找到的对象进行操作

# 改进后
# 1、定义定位属性到单独的元素定位器中：locator.py
# 2、在RegisterPage类中的根据元素定位属性创建元素对象
# 3、在RegisterPage类中的click_summit方法中：对对象直接进行操作

if __name__ == '__main__':
    register = RegisterPage()
    register.click_summit()













    def __init__(self):
        BasePage.__init__(self)
        self.__initObjects()

    def __initObjects(self):
        self.open()
        self.dictobjects = {}
        elements = self.find_elements(*self.inputs_loc)

        for e in elements:
            self.dictobjects[e.get_attribute('name')] = e
        self.dictobjects['sel_question'] = self.find_element(*self.sel_question_loc)
        self.dictobjects['submit'] = self.find_element(*self.submit_loc)

    def input_username(self,username):
        self.dictobjects['username'].send_keys(username)

    def input_password(self,password):
        self.dictobjects['password'].send_keys(password)

    def click_submit(self):
        # self.find_element(*self.submit_loc).click()
        self.dictobjects['submit'].click()

    def select_question(self):
        select = Select(self.dictobjects['sel_question'])
        index = random.randint(1,len(select.options))
        select.select_by_index(index)

if __name__ == '__main__':
    register = RegisterPage()
   

    register.input_username('huice008')
    register.select_question()
    time.sleep(5)
    register.close()